using System;
using System.IO;

namespace SoftwareMonkeys.Scriptech.IO
{
	public class DirectoryHelper
	{
		public static void CopyAll(string source, string target)
    	{
			CopyAll(new DirectoryInfo(source), new DirectoryInfo(target));
		}
		
		public static void CopyAll(string source, string target, string[] extensions)
    	{
			CopyAll(new DirectoryInfo(source), new DirectoryInfo(target), extensions);
		} 
		
		public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    	{
			CopyAll(source, target, new string[]{});
		}
		
		public static void CopyAll(DirectoryInfo source, DirectoryInfo target, string[] extensions)
    	{	
	
	        // Copy each file into it’s new directory.
	        foreach (FileInfo fi in source.GetFiles())
	        {		
				// If no extensions were provided (ie. copy all) OR the file extension matches one that was provided
				// then copy
				if (extensions == null || extensions.Length == 0
				    || Array.IndexOf(extensions, fi.Extension.Trim('.').ToLower()) > -1)
				{
					// Check if the target directory exists, if not, create it.
		        	if (Directory.Exists(target.FullName) == false)
		        	{
		            	Directory.CreateDirectory(target.FullName);
		        	}
					
	            	fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
					
				}
	        }
	
	        // Copy each subdirectory using recursion.
	        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
	        {
	            DirectoryInfo nextTargetSubDir =
	                target.CreateSubdirectory(diSourceSubDir.Name);
				
	            CopyAll(diSourceSubDir, nextTargetSubDir, extensions);
	        }
    	}
	}
}

